package com.lg.partition34;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/5 21:00
 */
public class P3366最小生成树 {

    private static final int N = 5010, M = 200010;
    private static Edge[] edges = new Edge[M];
    private static int[] parent = new int[N];
    private static int n, m;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        for (int i = 0; i <= n; i++) parent[i] = i;
        for (int i = 0; i < m; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
            edges[i] = new Edge(a, b, c);
        }
        long ans = kruskal();
        if (ans == -1) writer.write("orz\n");
        else writer.write(ans + "\n");
        writer.flush();
    }

    private static long kruskal() {
        Arrays.sort(edges, 0, m);
        int cnt = 0;
        long ans = 0;
        for (int i = 0; i < m; i++) {
            int u = edges[i].u, v = edges[i].v, w = edges[i].w;
            int x = find(u), y = find(v);
            if (x != y) {
                ans += w;
                parent[x] = y;
                cnt++;
            }
        }
        return cnt == n - 1 ? ans : -1;
    }

    private static int find(int u) {
        if (u != parent[u]) parent[u] = find(parent[u]);
        return parent[u];
    }

    private static class Edge implements Comparable<Edge> {
        private int u, v, w;

        public Edge(int u, int v, int w) {
            this.u = u;
            this.v = v;
            this.w = w;
        }

        @Override
        public int compareTo(Edge o) {
            return Integer.compare(w, o.w);
        }
    }
}
